package com.semp.gu.ui.elements;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.SwingWorker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.semp.gu.domain.local.LocalAlbumElement;
import com.semp.gu.domain.local.LocalAlbumElement.AlbumElementType;
import com.semp.gu.ui.UIConstants;
import com.semp.gu.ui.session.Session;
import com.semp.gu.ui.session.SessionAction;
import com.semp.gu.ui.session.SessionActionListener;

public class ImagePanel extends JPanel implements SessionActionListener, ActionListener, PropertyChangeListener {

	private Session				session;
	// Session
	private static final long	serialVersionUID	= 5522313771068375573L;
	// Logger
	private final static Logger	logger				= LoggerFactory.getLogger(ImagePanel.class);
	private JPanel				stateBar;
	private JLabel				stateLabel;
	private JProgressBar		loadingProgressBar;
	private JPanel				imageContentPanel;
	private JScrollPane			imageScrollPane;
	private final static int	ELEMENT_HGAP		= 10;
	private final static int	ELEMENT_VGAP		= 10;

	private List<ElementPanel>	elementPanelList;

	public ImagePanel() {
		super();
		/*
		 * Color
		 */
		this.setBackground(UIConstants.PANEL_IMAGES_COLOR);
		this.setLayout(new BorderLayout(0, 0));
		this.setSize(this.getMaximumSize());
		this.setPreferredSize(this.getMaximumSize());

		/*
		 * Inner components
		 */
		stateBar = new JPanel(new BorderLayout(2, 5));
		stateBar.setBackground(UIConstants.PANEL_IMAGES_COLOR);
		stateLabel = new JLabel();
		updateStatusLabel();
		stateBar.add(stateLabel, BorderLayout.WEST);
		loadingProgressBar = new JProgressBar();
		loadingProgressBar.setStringPainted(true);
		stateBar.add(loadingProgressBar, BorderLayout.EAST);
		this.add(stateBar, BorderLayout.SOUTH);
		loadingProgressBar.setVisible(false);

		/*
		 * Color
		 */

		imageContentPanel = new JPanel();
		imageContentPanel.setBackground(UIConstants.PANEL_IMAGES_COLOR);
		imageScrollPane = new JScrollPane(imageContentPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		this.add(imageScrollPane, BorderLayout.CENTER);

	}

	/*
	 * Called on file selection
	 */
	public void onContentChanged(SessionAction action) {
		if (action == SessionAction.LOCAL_ALBUM_SELECTED) {
			logger.debug("Local album selected, updated UI");

			DisplayElementsTask displayElementsTask = new DisplayElementsTask();
			displayElementsTask.addPropertyChangeListener(this);
			displayElementsTask.execute();

		}
	}
	
	private class DisplayElementsTask extends SwingWorker<Void, Void> {

		@Override
		protected Void doInBackground() throws Exception {
			loadingProgressBar.setVisible(true);
			/*
			 * Clean up
			 */
			imageContentPanel.removeAll();
			int nbColumns = imageContentPanel.getWidth() / (UIConstants.PANEL_ELEMENT_WIDTH + ELEMENT_HGAP );
			imageContentPanel.setLayout(new GridLayout(0, nbColumns,ELEMENT_HGAP, ELEMENT_VGAP));
			
			/*
			 * Element calculation
			 */
			List<LocalAlbumElement> elements = session.getLocalAlbum().getElements();
			elementPanelList = new ArrayList<ElementPanel>();

			int index = 1;
			int total = elements.size();
			for (LocalAlbumElement localAlbumElement : elements) {
				if (localAlbumElement.getType() != AlbumElementType.UNKNOWN) {
					ElementPanel elementPanel = new ElementPanel(localAlbumElement);
					elementPanelList.add(elementPanel);
					imageContentPanel.add(elementPanel);
					updateStatusLabel();
				}
				setProgress(index * 100 / total);
				index ++;
			}

			loadingProgressBar.setVisible(false);
			return null;
		}
		
	}

	public void propertyChange(PropertyChangeEvent evt) {
        if ("progress" == evt.getPropertyName()) {
            int progress = (Integer) evt.getNewValue();
            loadingProgressBar.setValue(progress);
            imageContentPanel.revalidate();
            stateLabel.revalidate();
        } 
	}
	

	private void updateStatusLabel() {
		int nbMovies = 0;
		int nbPictures = 0;
		if (elementPanelList != null) {
			for (ElementPanel elementPanel : elementPanelList) {
				switch (elementPanel.getLocalAlbumElement().getType()) {
				case MOVIE:
					nbMovies++;
					break;
				case PICTURE:
					nbPictures++;
					break;
				}
			}
		}
		stateLabel.setText(nbPictures + " pictures / " + nbMovies + " movies");
		stateLabel.repaint();
		logger.debug("Called updateUI on stateLabel");
	}

	// -------------------- Getters & setters
	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	// ------------------- Actions

	public void actionPerformed(ActionEvent e) {
		logger.warn("*******************Repainting panel");
		this.repaint();
	}

}
